//
//  XMGMeCell.m
//  01-百思不得姐
//
//  Created by xiaomage on 15/8/3.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "XMGMeCell.h"
@implementation XMGMeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        NSArray *square_list=@[
                               @{
                                   @"icon":@"guoji",
                                   @"name":@"中国客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"guonei",
                                   @"name":@"国际客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"jingzhukeyuan",
                                   @"name":@"精准客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"zidaokeyuan",
                                   @"name":@"自导客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"lianghaokeyuan",
                                   @"name":@"靓号客源",
                                   @"url":@"小明"
                                   }
                               ];
        
        NSArray *sqaures = [XMGSquare objectArrayWithKeyValuesArray:square_list];
        [self createSquares:sqaures];
        
    }
    return self;
}

/**
 * 创建方块
 */
- (void)createSquares:(NSArray *)sqaures
{
    // 一行最多4列
    int maxCols = 4;
    
    // 宽度和高度
    CGFloat buttonW = [UIScreen mainScreen].bounds.size.width / maxCols;
    CGFloat buttonH = buttonW;
    
    for (int i = 0; i<sqaures.count; i++) {
        // 创建按钮
        XMGSqaureButton *button = [XMGSqaureButton buttonWithType:UIButtonTypeCustom];
        // 监听点击
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        // 传递模型
        button.square = sqaures[i];
        [self addSubview:button];
        
        // 计算frame
        int col = i % maxCols;
        int row = i / maxCols;
        
        button.x = col * buttonW;
        button.y = row * buttonH;
        button.width = buttonW;
        button.height = buttonH;
    }
    
    // 8个方块, 每行显示4个, 计算行数 8/4 == 2 2
    // 9个方块, 每行显示4个, 计算行数 9/4 == 2 3
    // 7个方块, 每行显示4个, 计算行数 7/4 == 1 2
    // 总行数
    //    NSUInteger rows = sqaures.count / maxCols;
    //    if (sqaures.count % maxCols) { // 不能整除, + 1
    //        rows++;
    //    }
    
    // 总页数 == (总个数 + 每页的最大数 - 1) / 每页最大数
    
    NSUInteger rows = (sqaures.count + maxCols - 1) / maxCols;
    
    // 计算footer的高度
    self.height = rows * buttonH;
    
    NSLog(@" %f",self.height);
    // 重绘
    [self setNeedsDisplay];
}

- (void)buttonClick:(XMGSqaureButton *)button
{
    
    // 取出当前的导航控制器
    //    UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    //    UINavigationController *nav = (UINavigationController *)tabBarVc.selectedViewController;
    //    [nav pushViewController:web animated:YES];
}

// 设置背景图片
//- (void)drawRect:(CGRect)rect
//{
//    [[UIImage imageNamed:@"mainCellBackground"] drawInRect:rect];
//}

+(instancetype)cellForRow:(UITableView *)tableView{
    
    
    XMGMeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMGMeId"];
    
    
    return cell;
}
- (void)setFrame:(CGRect)frame
{
//    XMGLog(@"%@", NSStringFromCGRect(frame));
    frame.origin.y -= 1;
    [super setFrame:frame];
}

@end
